package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWmUserClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealNameMapper;
import com.heima.user.service.ApUserRealNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApUserRealNameServiceImpl extends ServiceImpl<ApUserRealNameMapper, ApUserRealname> implements ApUserRealNameService {

    @Autowired
    private ApUserRealNameMapper apUserRealNameMapper;

    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    public ResponseResult list(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        Page<ApUserRealname> realnamePage = new Page<>(page, size);
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper();
        if (dto.getStatus() != null) {
            wrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        Page<ApUserRealname> selectPage = apUserRealNameMapper.selectPage(realnamePage, wrapper);
        return PageResponseResult.okResult(selectPage.getRecords());


    }

    @Autowired
    private IWmUserClient wmUserClient;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult auth(AuthDto dto, Integer type) {
        // 0. 入参判空
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 1. 得到参数
        Integer id = dto.getId();
        // 2. 查询认证信息
        ApUserRealname apUserRealname = this.getById(id);
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3. 更新认证信息
        apUserRealname.setStatus((short) (type == 1 ? 9 : 2));
        apUserRealname.setReason(dto.getMsg());
        apUserRealname.setId(id);
        apUserRealNameMapper.updateById(apUserRealname);
        // 4. 通过
        if (type == 1) {
            // 4.1 获取用户名字
            String name = apUserRealname.getName();
            // 4.2 调用wemedia客户端新增用户
            Map<String, String> map = new HashMap<>();
            map.put("name", name);
            ResponseResult responseResult = wmUserClient.addUser(map);
            if (responseResult == null || responseResult.getCode() != 200) {
                throw new RuntimeException("自媒体用户创建失败");
            }
            // 4.3 更新APP用户的flag状态
            ApUser apUser = new ApUser();
            apUser.setId(apUserRealname.getUserId());
            apUser.setFlag((short) 1);
            int updateResult = apUserMapper.updateById(apUser);
            if (updateResult < 1) {
                throw new RuntimeException("APP用户状态更新失败");
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
